package com.nnnu.wsnackshop.utils;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

public class PasswordUtil {
    private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();

    /**
     * 对明文密码进行哈希
     * @param rawPassword 明文密码
     * @return 哈希后密码
     */
    public static String hash(String rawPassword) {
        return ENCODER.encode(rawPassword);
    }

    /**
     * 验证明文密码与哈希密码是否匹配
     * @param rawPassword 明文密码
     * @param encodedPassword 数据库中存储的哈希密码
     * @return 匹配返回 true，否则 false
     */
    public static boolean verify(String rawPassword, String encodedPassword) {
        if (rawPassword == null || encodedPassword == null) {
            return false;
        }
        return ENCODER.matches(rawPassword, encodedPassword);
    }
}
